home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-09-17 | 9.2 KB | 305 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: FWResAcc.cpp
- // Release Version: $ ODF 2 $
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "FWOS.hpp"
-
- #ifndef FWRESACC_H
- #include "FWResAcc.h"
- #endif
-
-
- #ifdef FW_BUILD_MAC
- #pragma segment fwresour
- #endif
-
- //========================================================================================
- // Template instantiation
- //========================================================================================
-
- #include "FWSOMPtr.tpp"
-
- FW_DEFINE_AUTO_TEMPLATE(FW_TCountedSOMPtr, FW_OResource)
-
- #if FW_USE_TEMPLATE_PRAGMAS
- #pragma template_access public
- #pragma template FW_TCountedSOMPtr<FW_OResource>
- #endif
-
- #if FW_ANSI_TEMPLATE_INSTANTIATION
- template class FW_TCountedSOMPtr<FW_OResource>;
- #endif
-
- //========================================================================================
- // CLASS FW_PResource
- //========================================================================================
-
- FW_DEFINE_AUTO(FW_PResource)
-
- //----------------------------------------------------------------------------------------
- // FW_PResource::FW_PResource
- //----------------------------------------------------------------------------------------
-
- FW_PResource::FW_PResource(Environment* ev,
- FW_OResourceFile* file,
- FW_ResourceID resourceID,
- FW_ResourceType resourceType)
- {
- FW_OResource* rep = new FW_OResource();
-
- SetRep(ev, rep);
- rep->InitFromFile(ev,
- file,
- resourceID,
- resourceType);
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PResource::FW_PResource
- //----------------------------------------------------------------------------------------
-
- FW_PResource::FW_PResource(const FW_PResource& other) :
- FW_TCountedSOMPtr<FW_OResource>(other)
- {
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PResource::FW_PResource
- //----------------------------------------------------------------------------------------
-
- FW_PResource::FW_PResource(Environment* ev, FW_OResource* rep) :
- FW_TCountedSOMPtr<FW_OResource>(ev, rep)
- {
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_PResource::~FW_PResource
- //----------------------------------------------------------------------------------------
-
- FW_PResource::~FW_PResource()
- {
- FW_START_DESTRUCTOR
- }
-
- //========================================================================================
- // CLASS FW_CAcquireResourceData
- //
- // A resource data acquistion helper object. This object can be used in order
- // to obtain access to resource data in an exception safe manner.
- //========================================================================================
-
- FW_DEFINE_AUTO(FW_CAcquireResourceData)
-
- //----------------------------------------------------------------------------------------
- // FW_CAcquireResourceData::FW_CAcquireResourceData
- //----------------------------------------------------------------------------------------
-
- FW_CAcquireResourceData::FW_CAcquireResourceData(Environment *ev, FW_OResource* resource) :
- fResource(ev, resource)
- {
- fData = fResource->AcquireData(ev);
- fSize = fResource->GetSize(ev);
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // FW_CAcquireResourceData::~FW_CAcquireResourceData
- //----------------------------------------------------------------------------------------
-
- FW_CAcquireResourceData::~FW_CAcquireResourceData()
- {
- FW_START_DESTRUCTOR
- FW_SOMEnvironment ev;
-
- fResource->ReleaseData(ev);
- }
-
- //========================================================================================
- // Global Utility Functions
- //========================================================================================
-
- #ifdef FW_BUILD_WIN
- #pragma pack(push,1)
- #endif
-
- struct FW_SMultiStringResEntry
- {
- short fStringID;
- unsigned short fStringOffset;
- };
-
- struct FW_SMultiStringHeader
- {
- unsigned short fStringCount; // number of entrys
- FW_SMultiStringResEntry fEntryArray[1]; // table of entries
- unsigned short GetStringOffset(short theKey);
- };
-
- #ifdef FW_BUILD_WIN
- #pragma pack(pop)
- #endif
-
- //----------------------------------------------------------------------------------------
- // FW_SMultiStringHeader::GetStringOffset
- //----------------------------------------------------------------------------------------
-
- static const unsigned short kBadOffset = 0xFFFF;
-
- unsigned short FW_SMultiStringHeader::GetStringOffset(short theKey)
- {
- short wLo = 0,
- wHi = fStringCount - 1;
- short wValue; // trial Value
- // X[wLo..wHi] are ordered, but untested
- do
- {
- short wMid = (wLo + wHi) / 2;
- wValue = fEntryArray[wMid].fStringID; // try middle Value
- if (wValue <= theKey)
- wLo = wMid + 1; // X[0..wLo-1] <= theKey
-
- if (wValue >= theKey)
- wHi = wMid - 1; // X[wHi+1..wMax] >= theKey X[wLo..wHi] untested
- } while (wLo <= wHi); // still at least one untested
-
- if (wLo - wHi == 2)
- return fEntryArray[wHi + 1].fStringOffset;
-
- return kBadOffset;
- }
-
- //========================================================================================
- // Global Functions
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // FW_HasStringByPosition
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_HasStringByPosition(Environment *ev,
- FW_PResourceFile &file,
- FW_ResourceID resourceID,
- FW_ResourceType resourceType,
- unsigned short position)
- {
- FW_PResource resource(ev, file, resourceID, resourceType);
- FW_CAcquireResourceData data(ev, resource);
- void *p = data.GetData();
-
- FW_SMultiStringHeader* head = (FW_SMultiStringHeader*) p;
-
- FW_ASSERT(position >= 0);
- FW_ASSERT(head->fStringCount > 0);
-
- return position < head->fStringCount;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_LoadStringByPosition
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_HasStringByID(Environment *ev,
- FW_PResourceFile &file,
- FW_ResourceID resourceID,
- FW_ResourceType resourceType,
- unsigned short id)
- {
- FW_PResource resource(ev, file, resourceID, resourceType);
- FW_CAcquireResourceData data(ev, resource);
- void *p = data.GetData();
-
- FW_SMultiStringHeader* head = (FW_SMultiStringHeader*) p;
- FW_ASSERT(head->fStringCount > 0);
-
- // Compute the Offset into the table, based on the position.
- const unsigned short offset = head->GetStringOffset(id);
- return (offset != kBadOffset);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_LoadStringByPosition
- //----------------------------------------------------------------------------------------
-
- void FW_LoadStringByPosition(Environment *ev,
- FW_PResourceFile &file,
- FW_ResourceID resourceID,
- FW_ResourceType resourceType,
- unsigned short position,
- FW_CString &string)
- {
- FW_PResource resource(ev, file, resourceID, resourceType);
- FW_CAcquireResourceData data(ev, resource);
- void *p = data.GetData();
-
- FW_SMultiStringHeader* head = (FW_SMultiStringHeader*) p;
- FW_ASSERT(head->fStringCount > 0);
-
- // Compute the Offset into the table, based on the position.
- const unsigned short offset = head->fEntryArray[position].fStringOffset;
-
- string.ReplaceAll(((const FW_Char*)p)+offset);
- }
-
- //----------------------------------------------------------------------------------------
- // FW_LoadStringByIDNoFail
- //----------------------------------------------------------------------------------------
-
- FW_Boolean FW_LoadStringByIDNoFail(Environment *ev,
- FW_PResourceFile &file,
- FW_ResourceID resourceID,
- FW_ResourceType resourceType,
- unsigned short id,
- FW_CString &string)
- {
- FW_Boolean success = TRUE;
-
- FW_TRY
- {
- ::FW_LoadStringByID(ev, file, resourceID, resourceType, id, string);
- }
- FW_CATCH_BEGIN
- FW_CATCH_EVERYTHING()
- {
- string.Truncate(0);
- success = FALSE;
- }
- FW_CATCH_END
-
- return success;
- }
-
- //----------------------------------------------------------------------------------------
- // FW_LoadStringByID
- //----------------------------------------------------------------------------------------
-
- void FW_LoadStringByID(Environment *ev,
- FW_PResourceFile &file,
- FW_ResourceID resourceID,
- FW_ResourceType resourceType,
- unsigned short id,
- FW_CString &string)
- {
- FW_PResource resource(ev, file, resourceID, resourceType);
- FW_CAcquireResourceData data(ev, resource);
- void *p = data.GetData();
-
- FW_SMultiStringHeader* head = (FW_SMultiStringHeader*) p;
- FW_ASSERT(head->fStringCount > 0);
-
- // Compute the Offset into the table, based on the position.
- const unsigned short offset = head->GetStringOffset(id);
- if (offset == kBadOffset)
- FW_Failure(FW_xStringResourceNotFound);
-
- string.ReplaceAll(((const FW_Char*)p)+offset);
- }
-
-